package com.wanmait.teatrain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanmait.teatrain.entity.CourseResource;
import com.wanmait.teatrain.entity.TrainingResource;
import com.wanmait.teatrain.entity.TrainingTag;
import com.wanmait.teatrain.mapper.AdminRoleMapper;
import com.wanmait.teatrain.mapper.CourseResourceMapper;
import com.wanmait.teatrain.mapper.TrainingResourceMapper;
import com.wanmait.teatrain.mapper.TrainingTagMapper;
import com.wanmait.teatrain.service.CourseResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.teatrain.utils.AjaxResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程与资源关联表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2025-10-22
 */
@Service
@Transactional
public class CourseResourceServiceImpl extends ServiceImpl<CourseResourceMapper, CourseResource> implements CourseResourceService {
    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Resource
    private CourseResourceMapper courseResourceMapper;
    @Resource
    private TrainingResourceMapper trainingResourceMapper;
    @Resource
    private TrainingTagMapper trainingTagMapper;
    @Override
    //根据用户权限id查显示相关资源
    public AjaxResult selectResourseByCourseId(Integer courseId, Integer adminId) {
        //根据adminId查出该id的roleId
        List<Integer> roleId = adminRoleMapper.selectAdminRole(adminId);

        System.out.println("查出的role权限数组" + roleId);
        //如果roleId数组中有2和3则为店长，可以查询所有的资源
        if (roleId != null && roleId.contains(2) && roleId.contains(3) && roleId.size() == 2){
            System.out.println("进入店长资源");
            LambdaQueryWrapper<CourseResource> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(courseId!=null,CourseResource::getCourseId,courseId);
            List<CourseResource> list = courseResourceMapper.selectList(lambdaQueryWrapper);
            //resourseId对应的资源
            list.forEach(list1->{
                list1.setTrainingResource(trainingResourceMapper.selectById(list1.getResourceId()));
            });
            //传出店长可见资源
            return AjaxResult.success().put("resourses",list);
        }
        //如果roleId只有3，则为店员，去除掉资源中的管理标签资源
        else if (roleId.size() == 1 && roleId.contains(3)){
            System.out.println("进入店员资源");
            //查出所有的资源管理list
            LambdaQueryWrapper<CourseResource> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(courseId!=null,CourseResource::getCourseId,courseId);
            List<CourseResource> list = courseResourceMapper.selectList(lambdaQueryWrapper);
            list.forEach(list1->{
                list1.setTrainingResource(trainingResourceMapper.selectById(list1.getResourceId()));
            });

            //查出管理的标签
            List<Integer> tagList = trainingTagMapper.selectTagByManage();
            System.out.println("标签列表" + tagList);
            //list的stream流中的trainResourse对象的tagId为该资源的标签，tagList为管理标签数组

            //排除 trainingResource 的 tagId 在 tagList 中的元素
            List<CourseResource> filteredList = list.stream()
                    .filter(obj -> !tagList.contains(obj.getTrainingResource().getTagId()))
                    .collect(Collectors.toList());
            //传出店员的可见资源
            return AjaxResult.success().put("resourses",filteredList);
        }
        //抛异常，全局异常 token异常 前端请求  重新登陆
        return AjaxResult.error("角色权限出现问题");
    }

    @Override
    public AjaxResult selectResource(Integer courseId) {
        QueryWrapper<CourseResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<CourseResource> list = courseResourceMapper.selectList(queryWrapper);
        return AjaxResult.success().put("resources",list);
    }
}
